package cn.wang.web.sysadmin;

import cn.wang.entity.sysadmin.*;
import cn.wang.service.sysadmin.DeptService;
import cn.wang.service.sysadmin.ModuleService;
import cn.wang.service.sysadmin.RoleService;
import cn.wang.service.sysadmin.UserService;
import cn.wang.utils.FastJsonUtil;
import cn.wang.utils.Pagination;
import cn.wang.web.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by john on 2017/11/1.
 */
@Controller
public class RoleAction extends BaseAction implements ModelDriven<Role> {
    private Pagination page = new Pagination();
    private List<Role> roleList;
    private List<Dept> deptList;
    private Role model = new Role();
    //json对象
    private String jsonStr;
    //获去来自页面的模块id
    private String moduleIds;
    @Autowired
    private RoleService roleService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private ModuleService moduleService;
    //分页
    public String list() {
        page = roleService.findPage(page.getPageNo(), page.getPageSize());
        page.setUrl("roleAction_list");
        push(page);
        return "list";
    }
    //添加用户前 先查询所有部门列表 所属领导
    public String tocreate() {
        roleList=roleService.findAll();
        deptList =deptService.findAll();
       return "tocreate";
    }
    //查寻单个部门信息
    public String toview() {
        model=roleService.findById(model.getId());
        push(model);
        return "toview";
    }
    //添加部门
    public String insert() {
        roleService.save(model);
        return "rlist";
    }
    //更新部门 先查询
    public String toupdate() {
        //查询所有部门
        deptList =deptService.findAll();
        model=roleService.findById(model.getId());
        push(model);
        //根据id查询老部门
        //移除本部门
        //roleList.remove(model);
        return "toupdate";
    }
    public String update() {
        roleService.update(model);
        return "rlist";
    }
    //删除部门 多个部门删除时 id 字符类型id会 拼接在一起(,空格)
    public String delete() {
        if (model.getId() == null) {
            return "rlist";
        }
        String[] ids = model.getId().split(", ");
        roleService.delete(ids);
        return "rlist";
    }
    //权限控制 z-tree 节点树 json  ajax;
    public String tomodule(){
        //只负责跳转
         model=roleService.findById(model.getId());
        push(model);
        return "tomodule";
    }
    public String roleModuleJsonStr(){
        //查询所有模块列表
        List<Module>moduleList=moduleService.findAll();
        //查询角色信息拥有的模块
        Role role=roleService.findById(model.getId());
        Set<Module> moduleSet=role.getModuleSet();

        //转换为json数据 先转换为模型数据
        //{id:206, pId:2, name:"Checkbox nocheck 演示", file:"excheck/checkbox_nocheck"},
        Set<ZtreeModule>ztreeModuleSet=new HashSet<>();
        for (Module module:moduleList){
            ZtreeModule ztreeModule=new ZtreeModule(module.getId(),module.getParentId(),module.getName(),false,true);
            //判断是否包含 包含即被选中
            if(moduleSet.contains(module)){
                ztreeModule.setChecked(true);
            }
            ztreeModuleSet.add(ztreeModule);
        }
        //将模型数据转为json
        jsonStr=FastJsonUtil.toJSONString(ztreeModuleSet);

        //通过response打印
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        try {
           PrintWriter out=response.getWriter();
            out.print(jsonStr);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return NONE;
    }
    //保存角色模块 页面得到的角色id 模块id字符串
    //需要通过id 查询出角色, 模块 并给角色设置id
    public String module(){
        //roleService.updateModule(model.getId(),moduleIds);
        Role role=roleService.findById(model.getId());
        String[] moduleids=moduleIds.split(",");
        Set<Module>moduleSet =new HashSet<>(moduleids.length);
        for (int i=0;i<moduleids.length;i++){

           Module module= moduleService.findById(moduleids[i]);
            moduleSet.add(module);
        }
        role.setModuleSet(moduleSet);
        roleService.update(role);
        return "rlist";
    }

    public void setPage(Pagination page) {
        this.page = page;
    }

    @Override
    public Role getModel() {
        //将字段名设为model使一行代码两用 1模型驱动 2 字段set方法
        return model;
    }
    public List<Role> getRoleList() {
        return roleList;
    }
    public List<Dept> getDeptList() {
        return deptList;
    }

    public void setModuleIds(String moduleIds) {
        this.moduleIds = moduleIds;
    }
}
